package vip.mate.message.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.alicp.jetcache.anno.EnableCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import vip.mate.core.common.api.Result;
import vip.mate.core.rocketmq.config.MqConfig;
import vip.mate.message.entiry.mongo.MongoDbMqEntiry;

import java.util.List;

/**
 * @author longzhe
 * @title: TestMongoDbController
 * @date 2022/1/20 17:24
 * @description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/mongodb")
public class TestMongoDbController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * basic：MQ 发送字符串消息
     *
     * @param msg : 发送内容
     * @return java.lang.Object
     * @throws
     * @Date 2021/4/25 3:58 下午
     */
    @RequestMapping("/save")
    public Object save(@RequestParam(value = "msg", required = true, defaultValue = "你好，RocketMQ") String msg) {
        // 同步发送
        SendResult sendResult = rocketMQTemplate.syncSend(MqConfig.Topic.TOPICSTRING, msg);
        log.info("MQ发送结果：{}", sendResult);

        MongoDbMqEntiry entiry = new MongoDbMqEntiry(sendResult.getMsgId(), JSONObject.toJSONString(sendResult), msg, DateUtil.date());
        mongoTemplate.save(entiry);
        return sendResult;
    }


    /**
     * https://www.cnblogs.com/yangxiaohui227/p/11353803.html
     * mongodb 高级查询
     *
     * @return
     */
    @RequestMapping("/query")
    public Result query() {
        //List<MongoDbMqEntiry> list = mongoTemplate.findAll(MongoDbMqEntiry.class);
        Query query = new Query();
        Criteria criteria = Criteria.where("createTime").lte(DateUtil.date());
        query.addCriteria(criteria);
        //排序
        query.with(Sort.by(Sort.Order.desc("createTime")));
        List<MongoDbMqEntiry> list2 = mongoTemplate.find(query, MongoDbMqEntiry.class);
        return Result.data(list2);
    }

    /**
     * https://www.cnblogs.com/yinjihuan/p/10668695.html
     * jetcache 使用
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryById")
    @Cached(name = "queryById.", key = "#id", expire = 20, cacheType = CacheType.BOTH)
    public Result queryById(String id) {
        Query query = new Query();
        Criteria criteria = Criteria.where("createTime").lte(DateUtil.date());
        query.addCriteria(criteria);
        //排序
        query.with(Sort.by(Sort.Order.desc("createTime")));
        MongoDbMqEntiry entiry = mongoTemplate.findOne(query, MongoDbMqEntiry.class);
        return Result.data(entiry);
    }
}
